package edu.spec.student.controller;

import edu.spec.student.model.questionnaire.Subject;
import edu.spec.student.service.iface.ISubjectService;
import edu.spec.basic.model.dto.MvcReturn;
import org.madgecko.basic.util.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * Author       : MadFrog
 * Date         : 2018/7/6 14:02
 * Description  :
 */
@Controller
public class SubjectController {

    @Inject
    private ISubjectService subjectService;

    @ResponseBody
    @RequestMapping("/student/subject/listSubjectByQuestionnaire")
    public MvcReturn listSubject(HttpSession session){
        Integer questionnaireId = (Integer) session.getAttribute("questionnaireId");
        List<Subject> subjectList = subjectService.listSubjectByQuestionnaire(questionnaireId);
        return new MvcReturn(true, subjectList);
    }

    @RequestMapping(value="/admin/questionnaire/subject/importIn", method= RequestMethod.POST)
    public void importRubric(Integer questionnaireId, MultipartFile file, HttpServletRequest req, HttpServletResponse resp){
        try {
            boolean b = subjectService.importIn(questionnaireId, file, req);
            MvcReturn er = new MvcReturn(b);
            resp.setContentType("text/html;charset=UTF-8");
            resp.getWriter().write(JsonUtil.getInstance().obj2json(er));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ResponseBody
    @RequestMapping("/admin/questionnaire/subject/listByQuestionnaire")
    public MvcReturn listSubject(Integer questionnaireId){
        List<Subject> subjectList = subjectService.listSubjectByQuestionnaire(questionnaireId);
        return new MvcReturn(true, subjectList);
    }

}
